package com.esiran.payadmin.shiro.realm;

import com.esiran.payadmin.shiro.token.AdminAuthToken;
import com.esiran.payadmin.shiro.token.MultipleRealmToken;
import com.esiran.payadmin.util.JWTUtil;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

@Component
public class SystemUserRealm extends MultipleBaseRealm {
    @Override
    public String realmName() {
        return "adminRealm";
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        Set<String> p = new HashSet<>();
        p.add("a");
        p.add("b");
        p.add("c");
        authorizationInfo.setStringPermissions(p);
        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        MultipleRealmToken mt = (MultipleRealmToken) token;
        String userName = mt.getUsername();
        if (!userName.equals("admin_")){
            throw new UnknownAccountException("用户不存在");
        }
        String defaultPassword = "1eea36fbd4f4919251e3192dce2da380";
        String password = mt.passwordString();
        return new SimpleAuthenticationInfo(userName,password,getName());
    }

}
